Git & GitHub Mastery

From first commit to advanced collaboration: internalize version control mental models, branching strategies, rebasing, code review discipline, release management, and contribution excellence.

🧠Concept β†’ Model
πŸ”„Branch Strategy
⚠️Conflict Handling
πŸš€Release Flow
🀝Collaboration

1. Core Concepts

Internal state machine thinking > memorizing commands.

Repository Object Model

Git stores snapshots (blobs) referenced by trees, pointed to by commits. Branch = movable pointer to a commit. HEAD = current reference.

blobstreescommitsrefs

Staging Area (Index)

Acts as a curated snapshot in progress. You choose git add granularity β†’ commit exactly what matters.

indexgranular

Three States

Modified (working tree) β†’ Staged (index) β†’ Committed (repository). Understand transitions.

lifecyclemental model

Hashes = Content Identity

Commit IDs immutably bind content + parents + author + time. Rewriting history creates new hashes.

sha-1integrity

Essential First Commands

git init
git status
git add README.md
git commit -m "feat: initial commit"
# Link remote
git remote add origin https://github.com/user/repo.git
git push -u origin main

Clean Mental Model

  • Always know: (a) current branch, (b) staged vs unstaged diff, (c) upstream status.
  • Use git status + git log --oneline --graph --decorate --all as a dashboard.
  • Commit logically isolated changes: one concept per commit.
  • Write conventional commits or a consistent style for readable history.

Recoverable Safety Net

  • Use git reflog to recover from resets/amend mistakes.
  • Never panicβ€”refs & objects persist until GC.
  • Create WIP commits or stashes before risky operations.

2. Branching & Merging

Branch strategy = collaboration contract.

Core Branch Styles

  • Trunk-Based: Short-lived branches; rebase often, merge fast.
  • Git Flow: Long-lived develop, separate release/hotfix branches.
  • Release Train: Cadence-based release branch; cherry-pick urgent fixes.

Choosing Strategy

  • High velocity + CI maturity β†’ trunk-based.
  • Complex staged releases β†’ Git Flow.
  • Multiple versions in production β†’ release branches.

Merge vs Rebase

Feature branch history (rebased):
A -- B -- C (main)
            \
             d' -- e' -- f' (feature)

Feature branch history (merged):
A -- B -- C (main) -- M
            \        /
             d -- e -- f (feature)

When to Rebase

  • Clean linear feature development.
  • Sync with latest main before PR.
  • Squash fixups privately.

When to Merge

  • Public shared branches.
  • Preserve real conflict history.
  • Auditing large integration points.

Conflict Resolution Flow

# After pulling or rebasing
# 1. Examine status
git status
# 2. Open conflict markers in files
# 3. Decide keep ours/theirs or manual merge
# 4. Stage resolved files
git add file.js
# 5. Continue
# merge: git commit
# rebase: git rebase --continue
# 6. Final validation tests + push

3. Collaboration Workflows

PR quality & review discipline accelerate teams.

Pull Request Hygiene

Atomic scope, descriptive title, clear context, test evidence, checklist of impacts.

reviewsquality

Commit Message Conventions

Example: feat(parser): support streaming tokens – type(scope): summary.

semanticchangelog

Issue-Driven Flow

Map each change to a tracked issue β†’ traceability, planning accuracy, automated closure.

traceplanning

Code Review Mindset

Optimize for clarity, risk reduction, and teaching. Ask why not just what changed.

mentorshipsafety

Release Cadence

  • Automate version bump + changelog generation.
  • Tag annotated releases: git tag -a v1.4.0 -m "Release v1.4.0"
  • Immutable artifacts: keep reproducible builds.

Security & Compliance

  • Mandatory signed commits (GPG) for privileged repos.
  • CODEOWNERS enforce critical path review.
  • Secret scanning & dependency alerts active.

4. Advanced Toolkit

Rewrite, optimize, debug repository state with precision.

Interactive Rebase Power

Reorder, squash, reword commits: git rebase -i HEAD~7 β†’ curated history.

historysquash

Bisect for Bug Isolation

Binary search history: git bisect start β†’ mark good/bad commits β†’ pinpoint regression.

debugregression

Submodules & Alternatives

Use sparingly. Prefer package registries or monorepo subsets. If used: pin SHAs, document update flow.

composition

Cherry-Pick Discipline

Single commit forward-port/backport. Keep metadata; avoid divergence storms.

hotfixpatch

RefLog Rescue

# Oops after reset --hard
# 1. Show movement history
git reflog
# 2. Checkout lost commit
git checkout <commit-sha>
# 3. Restore branch pointer
git branch recovered-work
# 4. Continue safely

Squash Strategy

# Interactive rebase last N commits
git rebase -i HEAD~5
# Change 'pick' to 'squash' or 'fixup' accordingly
# Force push only if branch not shared or after team agreement

5. Interactive Practice Lab

Experiment with mental models locally in your browser.

Commit Graph Builder

Enter commit messages separated by newlines. We'll simulate a linear history; then branch.

(graph output)

Conflict Merge Simulator

Simulates conflicting edits to same line.

(conflict markers)

Rebase vs Merge Explainer

(explanation)

6. Command Cheat Sheet

High-frequency reference organized by intent.

Initialize / Config

git config --global user.name "Your Name"
git config --global user.email you@example.com
git init
.gitignore

Explore History

git log --oneline --graph --decorate --all
git show <commit>
git diff HEAD~2..HEAD

Change Flow

git add -p
git commit -m "feat: msg"
git restore --staged file.js
git stash push -m "ctx"
git stash list

Branch / Integrate

git switch -c feature/x
git merge main
git rebase main
git cherry-pick <sha>

Rewrite / Recover

git commit --amend
git reset --soft HEAD~1
git revert <sha>
git reflog

Clean / Optimize

git gc --aggressive --prune=now
git clean -fd

7. Review & Preparation

Solidify mastery through reflective practice.

Progress Checklist

Concept Q&A

Why is rebasing a shared branch risky?

It rewrites commit hashes; collaborators' histories diverge causing forced pushes & potential lost work if not coordinated.

Difference between reset --soft, --mixed, --hard?

Soft: move HEAD only; index + working keep changes. Mixed (default): reset index, keep working tree. Hard: reset index & working β†’ discards changes.

When prefer revert over reset?

On public history: revert creates a new commit negating changes preserving auditability; reset rewrites history.

Why sign commits?

Authenticity & non-repudiation; prevents impersonation in sensitive repos.